package com.ruoyi.web.controller.equ;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.equ.domain.EquStockin;
import com.ruoyi.equ.domain.vo.EquStockinVos;
import com.ruoyi.equ.domain.vo.StockinAndAssetVo;
import com.ruoyi.equ.service.IEquStockinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * 验收入库Controller
 *
 * @author ruoyi
 * @date 2025-11-05
 */
@RestController
@RequestMapping("/equ/stockin")
public class EquStockinController extends BaseController {
    @Autowired
    private IEquStockinService equStockinService;

    /**
     * 查询验收入库列表
     */
    @PreAuthorize("@ss.hasPermi('equ:stockin:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ModelAttribute EquStockin equStockin) {
        startPage();
        List<EquStockin> list = equStockinService.selectEquStockinList(equStockin);
        return getDataTable(list);
    }
    /**
     * 查询验收入库列表
     */
    @PreAuthorize("@ss.hasPermi('equ:stockin:list')")
    @GetMapping("/listVo")
    public TableDataInfo listVo(EquStockinVos equStockin) {
        startPage();
        List<EquStockinVos> list = equStockinService.selectEquStockinVoList(equStockin);
        return getDataTable(list);
    }
    /**
     * 导出验收入库列表
     */
    @PreAuthorize("@ss.hasPermi('equ:stockin:export')")
    @Log(title = "验收入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ModelAttribute EquStockin equStockin) {
        List<EquStockin> list = equStockinService.selectEquStockinList(equStockin);
        ExcelUtil<EquStockin> util = new ExcelUtil<EquStockin>(EquStockin.class);
        util.exportExcel(response, list, "验收入库数据");
    }

    /**
     * 获取验收入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('equ:stockin:query')")
    @GetMapping(value = "/{stockId}")
    public AjaxResult getInfo(@PathVariable("stockId") Long stockId) {
        return success(equStockinService.selectEquStockinByStockId(stockId));
    }

    /**
     * 新增验收入库
     */
    @PreAuthorize("@ss.hasPermi('equ:stockin:add')")
    @Log(title = "验收入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquStockin equStockin) {
        return toAjax(equStockinService.insertEquStockin(equStockin));
    }

    /**
     * 修改验收入库
     */
    @PreAuthorize("@ss.hasPermi('equ:stockin:edit')")
    @Log(title = "验收入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquStockin equStockin) {
        return toAjax(equStockinService.updateEquStockin(equStockin));
    }

    /**
     * 删除验收入库
     */
    @PreAuthorize("@ss.hasPermi('equ:stockin:remove')")
    @Log(title = "验收入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{stockIds}")
    public AjaxResult remove(@PathVariable("stockIds") Long[] stockIds) {
        return toAjax(equStockinService.deleteEquStockinByStockIds(stockIds));
    }


    /**
     * 2.2入库生成台账记录及新增
     */
    @PreAuthorize("@ss.hasPermi('equ:stockin:add')")
    @Log(title = "验收入库", businessType = BusinessType.INSERT)
    @PostMapping("/finishStockinAndAsset")
    public AjaxResult finishStockinAndAsset(@RequestBody StockinAndAssetVo stockinAndAssetVo) {
        try {
            Boolean result = equStockinService.finishStockinAndAsset(stockinAndAssetVo);
            return AjaxResult.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("操作失败");
        }
    }
    /**
     * 查询验收入库列表
     */
    @PreAuthorize("@ss.hasPermi('equ:stockin:list')")
    @GetMapping("/getStockinByAssetId/{assetId}")
    public TableDataInfo getStockinByAssetId(@PathVariable("assetId") Long assetId)
    {
        startPage();
        List<EquStockin> list = equStockinService.getStockinsByAssetId(assetId);
        return getDataTable(list);
    }

}
